package example.simple_mindmap.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import example.simple_mindmap.common.config.RedisProperties;
import example.simple_mindmap.common.domain.po.Result;
import example.simple_mindmap.common.enums.ResultCode;
import example.simple_mindmap.common.utils.DataUtil;
import example.simple_mindmap.common.utils.SessionHolder;
import example.simple_mindmap.chache.MindMapCache;
import example.simple_mindmap.config.MindMapProperties;
import example.simple_mindmap.domain.dto.MindMapListDTO;
import example.simple_mindmap.domain.po.MindMap;
import example.simple_mindmap.domain.po.Record;
import example.simple_mindmap.mapper.MindMapMapper;
import example.simple_mindmap.mapper.RecordMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService{
    private final RedisProperties redisProperties;
    private final MindMapCache mindMapCache;
    private final MindMapMapper mindMapMapper;
    private final MindMapProperties mmProperties;

    public Result getAllCollect(){
        List<Record> records = lambdaQuery().eq(Record::getUid, SessionHolder.getUser()).eq(Record::getIsCollect,true).list();
        if(records == null ){
            return Result.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        return Result.success(records);
    }


    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 点赞
     * @Describe: TODO
     **/
     public Result like(Long mid){
        if(DataUtil.isNaturalNumInvalid(mid)){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        MindMap mindMap = mindMapMapper.selectById(mid);
        if(mindMap == null){
            return Result.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        Record record =lambdaQuery().eq(Record::getMid,mid).eq(Record::getUid, SessionHolder.getUser()).one();
        if(record == null){
            record = new Record();
            record.setUid(SessionHolder.getUser());
            record.setIsCollect(false);
            record.setMid(mid);
            record.setCreateTime(new Date());
        }
        record.setIsLike(true);
        saveOrUpdate(record);

        mindMap.setLike((mindMap.getLike()+1));
        mindMapMapper.updateById((mindMap));

        //缓存
         mindMapCache.addOrUpdateMindMap1(mindMap);

        return Result.success(true);
    }
    /* ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 计算分数
     * @Describe: TODO
     **/
    private Double scoring1(MindMapListDTO mindMapListDTO){
        return mindMapListDTO.getLike() * mmProperties.like_score
                + mindMapListDTO.getCollect() * mmProperties.collect_score
                + mindMapListDTO.getView() * mmProperties.view_score;
    }
    /* ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 计算分数
     * @Describe: TODO
     **/
    private Double scoring2(MindMapListDTO mindMapListDTO){
        return (mindMapListDTO.getIsTop()==1? new Date().getTime():0)*1.0+mindMapListDTO.getUpdateTime().getTime();
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 取消点赞
     * @Describe: TODO
     **/
     public Result unLike(Long mid){
        if(DataUtil.isNaturalNumInvalid(mid)){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

         MindMap mindMap = mindMapMapper.selectById(mid);

         if(mindMap == null){
             return Result.failed(ResultCode.FAILED_NOT_EXISTS);
         }

        Record record = lambdaQuery().eq(Record::getUid, SessionHolder.getUser()).eq(Record::getMid, mid).one();
        if(record == null){
            return Result.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        record.setIsLike(false);
        saveOrUpdate(record);

         mindMap.setLike((mindMap.getLike()-1));
         mindMapMapper.updateById((mindMap));

         //缓存
         mindMapCache.addOrUpdateMindMap1(mindMap);

        return Result.success(true);
    }

    public Result collect(Long mid){
        if(DataUtil.isNaturalNumInvalid(mid)){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        MindMap mindMap = mindMapMapper.selectById(mid);

        if(mindMap == null){
            return Result.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        Record record =lambdaQuery().eq(Record::getMid,mid).eq(Record::getUid, SessionHolder.getUser()).one();
        if(record == null){
            record = new Record();
            record.setUid(SessionHolder.getUser());
            record.setIsLike(false);
            record.setMid(mid);
            record.setCreateTime(new Date());
        }
        record.setIsCollect(true);
        saveOrUpdate(record);

        mindMap.setCollect((mindMap.getCollect()+1));
        mindMapMapper.updateById((mindMap));

        //缓存
        mindMapCache.addOrUpdateMindMap1(mindMap);

        return Result.success(true);
    }

    public Result unCollect(Long mid){
        if(DataUtil.isNaturalNumInvalid(mid)){
            return Result.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        MindMap mindMap = mindMapMapper.selectById(mid);

        if(mindMap == null){
            return Result.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        Record record = lambdaQuery().eq(Record::getUid, SessionHolder.getUser()).eq(Record::getMid, mid).one();
        if(record == null){
            return Result.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        record.setIsCollect(false);
        saveOrUpdate(record);

        mindMap.setCollect((mindMap.getCollect()-1));
        mindMapMapper.updateById((mindMap));

        //缓存
        mindMapCache.addOrUpdateMindMap1(mindMap);

        return Result.success(true);
    }
    
    
}
